#include "genre.h"
#include "options.h"
#include "bdd.h"

genre::genre()
{
}

int genre::Existe(int codeAllocine)
{
    options opt;
    bdd db = bdd(opt.nomBdd());
    QString requete = "SELECT idGenre FROM genre WHERE codeAllocineGenre ="
            + db.TexteVersSql(QString::number(codeAllocine));
    return db.EstDansLaBase(requete);
}

void genre::Enregistre()
{
    options opt;
    bdd db = bdd(opt.nomBdd());
    QString requete = "INSERT INTO genre(idGenre, codeAllocineGenre, nomGenre) VALUES("
            "NULL, "
            +db.TexteVersSql(QString::number(this->codeAllocineGenre))+ ", "
            +db.TexteVersSql(this->nomGenre)+")";
    db.Execute(requete);
}

void genre::Supprime()
{
    options opt;
    bdd db = bdd(opt.nomBdd());
    QString requete = "DELETE FROM genre WHERE idGenre="+db.TexteVersSql(QString::number(this->idGenre));
    db.Execute(requete);
}

QList<genre> genre::ListeGenres()
{
    options opt;
    bdd db = bdd(opt.nomBdd());
    QString requete = "SELECT idGenre, codeAllocineGenre, nomGenre FROM genre ORDER BY nomGenre";
    db.Execute(requete);

    QList<genre> listeGenres;

    while(db.Requete().next())
    {
        genre unGenre;
        unGenre.setId(db.Requete().value(0).toInt());
        unGenre.setCodeAllocine(db.Requete().value(1).toInt());
        unGenre.setNom(db.Requete().value(2).toString());
        listeGenres << unGenre;

    }
    return listeGenres;
}

int genre::id()
{
    return this->idGenre;
}

void genre::setId(int id)
{
    this->idGenre = id;
}

void genre::setCodeAllocine(int code) // != id : code Allocine
{
    this->codeAllocineGenre = code;
}

int genre::codeAllocine()
{
    return this->codeAllocineGenre;
}

void genre::setNom(QString nom)
{
    this->nomGenre = nom;
}

QString genre::nom()
{
    return this->nomGenre;
}
